﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace SvnLightClient
{
    public partial class Config : Form
    {
        public Configuration AppConfiguration;
        private DataTable _configTable;

        public Config()
        {
            InitializeComponent();
        }

        private void Config_Load(object sender, EventArgs e)
        {
            //var configurationName = this.GetType().Assembly.CodeBase + ".config";
            var appSettings = AppConfiguration.AppSettings;
            _configTable = new DataTable();
            _configTable.Columns.Add("Key", typeof(string));
            _configTable.Columns.Add("Value", typeof(string));
            foreach (var key in appSettings.Settings.AllKeys)
                _configTable.Rows.Add(key, appSettings.Settings[key].Value);
            _configTable.AcceptChanges();
            this.dataGridView1.DataSource = _configTable;
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AutoResizeColumns();
            this.dataGridView1.Columns[0].ReadOnly = true;
            this.dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Silver;
            this.dataGridView1.AllowUserToResizeRows = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var changes = _configTable.GetChanges();
            if (changes != null)
            {
                foreach (DataRow row in changes.Rows)
                {
                    AppConfiguration.AppSettings.Settings.Remove(row["Key"].ToString());
                    AppConfiguration.AppSettings.Settings.Add(row["Key"].ToString(), row["Value"].ToString());
                }
                AppConfiguration.Save();
            }
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
